/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ordenacao;

/**
 *
 * @author Charles Henrique Porto Ferreira
 * @author jurandir Jardin Neto
 */
public class SimpleSort {

    public static void bubbleSort(int vetor[]) {
        int j;
        boolean flag = true;   // set flag to true to begin first pass
        int temp;   //holding variable

        while (flag) {
            flag = false;    //set flag to false awaiting a possible swap
            for (j = 0; j < vetor.length - 1; j++) {
                if (vetor[ j] > vetor[j + 1]) // change to > for ascending sort
                {
                    temp = vetor[ j];                //swap elements
                    vetor[ j] = vetor[ j + 1];
                    vetor[ j + 1] = temp;
                    flag = true;              //shows a swap occurred  
                }
            }
        }

    }

    public static void selectionSort(int vetor[]) {

        int i, j, first, temp;
        for (i = vetor.length - 1; i > 0; i--) {
            first = 0;   //initialize to subscript of first element
            for (j = 1; j <= i; j++) //locate smallest element between positions 1 and i.
            {
                if (vetor[ j] > vetor[ first]) {
                    first = j;
                }
            }
            temp = vetor[ first];   //swap smallest found with element in position i.
            vetor[ first] = vetor[ i];
            vetor[ i] = temp;
        }
    }

    public static void insertionSort(int vetor[]) {
        int j;     // the number of items sor`ted so far
        int key;   // the item to be inserted
        int i;

        for (j = 1; j < vetor.length; j++) // Start with 1 (not 0)
        {
            key = vetor[ j];
            for (i = j - 1; (i >= 0) && (vetor[ i] > key); i--) // Smaller values are moving up
            {
                vetor[ i + 1] = vetor[ i];
            }
            vetor[ i + 1] = key;    // Put the key in its proper location
        }
    }
}
